using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerObject : OBJBase, IObjectMove, ILookToCamera//, IReversal
{
    GameObject obj;
    GameObject anima;
    GameObject rota;
    bool isMove;
    Vector3 objDir;
    Vector3 endPos;
    public Vector3 ObjDir { get => objDir; set => objDir = value; }
    public Vector3 EndPos { get => endPos; set => endPos = value; }
    public GameObject Obj { get => obj; set => obj = value; }
    public bool IsMove { get => IsMove1; set => IsMove1 = value; }
    public bool IsMove1 { get => isMove; set => isMove = value; }

    public PlayerObject()
    {
        Obj = GameObject.Instantiate<GameObject>(this.ResourceObject("ObjectRes/PlayerOBJ"), Vector3.zero, Quaternion.identity);
        rota = Obj.transform.Find("Rotation").gameObject;
        anima = Obj.transform.Find("Rotation/PlayerAnima").gameObject;
        CameraContorller.GetInstance().LookAtPlayer(Obj.transform.position);
        EventService.AddEventListener<Vector3, Vector3, bool>(E_Event_Type.PlayerMove, SetVetc);
        MonoMgr.GetInstance().AddUpdateListener(Move);
        LookToCamera();
    }

    void SetVetc(Vector3 dir, Vector3 end, bool move)
    {
        ObjDir = dir;
        EndPos = end;
        IsMove1 = move;
    }

    public void Move()//接口方法 移动逻辑
    {
        if (IsMove)
        {
            if (Vector3.Distance(Obj.transform.position, EndPos) > 0.05f)
            {
                Obj.transform.Translate(ObjDir.normalized * 5 * Time.deltaTime);
            }
            else
            {
                Obj.transform.position = EndPos;
                IsMove = false;
            }
        }
    }

    public void LookToCamera()
    {
        Obj.transform.position = new Vector3(0, 0.05f, 0);
        rota.transform.eulerAngles = new Vector3(CameraContorller.GetInstance().LookCamera, 0, 0);
    }


    public void Recycle()
    {
        Obj.transform.gameObject.SetActive(false);
        GameObject.Destroy(Obj);
        EventService.RemoveEventListener<Vector3, Vector3, bool>(E_Event_Type.PlayerMove, SetVetc);
        MonoMgr.GetInstance().RemoveUpdateListener(Move);

    }
}
